package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.finance.domain.TFreightBillInfo;
import com.ruoyi.finance.domain.TProfitBillInfo;
import com.ruoyi.finance.domain.TSalaryBillInfo;
import com.ruoyi.finance.service.*;
import com.ruoyi.waybill.domain.TWayBillExt;
import com.ruoyi.waybill.service.ITWayBillExtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.time.LocalDate;

import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 定时任务，利润账单，任务
 * 
 * @author ruoyi
 */
@Component("pbTask")
public class PbTask
{
    @Autowired
    private ITWayBillExtService tWayBillExtService;

    @Autowired
    private ITProfitBillInfoService tProfitBillInfoService;

    @Autowired
    private ITProfitWayBillRelService tProfitWayBillRelService;

    private static Logger logger = LoggerFactory.getLogger(FbTask.class);

    /**
     * 统计月利润单，无参
     */

    public void statisticMonthProfitBill()
    {
        logger.debug("统计，月利润单，月账单，按结算客户，开始 ...");
        /**
         * 计算当前日期，前一月日期
         */
        LocalDate currentDate = LocalDate.now();
        logger.debug("当前日期: " + currentDate);
        LocalDate lastMonthDate = currentDate.minusMonths(1);
        logger.debug("上月日期: " + lastMonthDate);
        LocalDate lastMonthFirstDay = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        logger.debug("上个月第一天：" + lastMonthFirstDay);
        LocalDate lastMonthLastDay = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        logger.debug("上个月最后一天：" + lastMonthLastDay);

        TWayBillExt tWayBillExt = new TWayBillExt();
        Double monthFreightWeightSum = 0.0;
        Double monthFreightAmountSum = 0.0;
        Double monthSalaryAmountSum = 0.0;
        Double monthCashSupplementGasSum = 0.0;
        Double monthAddGasFeeSum = 0.0;
        Double monthTollFeeCashSum = 0.0;
        Double monthTollFeeEtcSum = 0.0;
        Double monthForfeitSum = 0.0;
        Double monthInformationFeeSum = 0.0;
        Double monthRepairTruckFeeSum = 0.0;
        Double monthServiceStationRepairSum = 0.0;
        Double monthInsurancePremiumSum = 0.0;
        Double monthTireFeeSum = 0.0;
        Double monthOtherFeeSum = 0.0;
        Double monthExpenseTotalSum = 0.0;
        Double monthProfitSum = 0.0;
        TProfitBillInfo tProfitBillInfo = new TProfitBillInfo();
        String monthProfitBillNumber;
        Long monthProfitBillId = 0L;
        List<Long> wayBillIds = new ArrayList<>();

        /**
         * 按录单日期，筛选结费客户列表
         */
        List<String> freightSettlementCustomers = tWayBillExtService.selectStatisticFieldByRecordDate(1 /*结算客户*/, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
//        logger.debug("结费客户列表：freightSettlementCustomers = " + freightSettlementCustomers);

        /**
         * 遍历结算客户，运单
         */
        for(String freightSettlementCustomer : freightSettlementCustomers)
        {
            /**
             * 按车牌号。录单日期，查询运单
             */
            tWayBillExt.setFreightSettlementCustomer(freightSettlementCustomer);
            logger.debug("结算客户：freightSettlementCustomer = " + freightSettlementCustomer);
            List<TWayBillExt> wayBillExts = tWayBillExtService.selectTWayBillExtListUsedByMonthStatistic(1 /*车牌号码*/, tWayBillExt, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
//            logger.debug("运单列表：wayBillExts = " + wayBillExts);
            /**
             * 合计利润，记录运单ID
             */
            for(TWayBillExt wayBillExt : wayBillExts) {
                monthFreightWeightSum = monthFreightWeightSum + wayBillExt.getFreightWeight();
                monthFreightAmountSum = monthFreightAmountSum + wayBillExt.getFreightAmount();
                monthSalaryAmountSum = monthSalaryAmountSum + wayBillExt.getSalaryAmount();
                monthCashSupplementGasSum = monthCashSupplementGasSum + wayBillExt.getCashSupplementGas();
                monthAddGasFeeSum = monthAddGasFeeSum + wayBillExt.getAddGasFee();
                monthTollFeeCashSum = monthTollFeeCashSum + wayBillExt.getTollFeeCash();
                monthTollFeeEtcSum = monthTollFeeEtcSum + wayBillExt.getTollFeeEtc();
                monthForfeitSum = monthForfeitSum + wayBillExt.getForfeit();
                monthInformationFeeSum = monthInformationFeeSum + wayBillExt.getInformationFee();
                monthRepairTruckFeeSum = monthRepairTruckFeeSum + wayBillExt.getRepairTruckFee();
                monthServiceStationRepairSum = monthServiceStationRepairSum + wayBillExt.getServiceStationRepair();
                monthInsurancePremiumSum = monthInsurancePremiumSum + wayBillExt.getInsurancePremium();
                monthTireFeeSum = monthTireFeeSum + wayBillExt.getTireFee();
                monthOtherFeeSum = monthOtherFeeSum + wayBillExt.getOtherFee();
                monthExpenseTotalSum = monthExpenseTotalSum + wayBillExt.getExpenseTotal();
                monthProfitSum = monthProfitSum + wayBillExt.getProfit();
                wayBillIds.add(wayBillExt.getWayBillId());
            }

            /**
             * 保护，goodsIds，为空，直接退出
             */
            if(wayBillIds.isEmpty()) return;

            /**
             * 月利润单编号
             */
            monthProfitBillNumber = OrderUtils.getOrderNumber("MP", "-");

            /**
             * 利润总额，四舍五入，保留2位小数
             */
            monthFreightWeightSum = Math.round(monthFreightWeightSum * 1000.0) / 1000.0;
            monthFreightAmountSum = Math.round(monthFreightAmountSum * 100.0) / 100.0;
            monthSalaryAmountSum = Math.round(monthSalaryAmountSum * 100.0) / 100.0;
            monthCashSupplementGasSum = Math.round(monthCashSupplementGasSum * 100.0) / 100.0;
            monthAddGasFeeSum = Math.round(monthAddGasFeeSum * 100.0) / 100.0;
            monthTollFeeCashSum = Math.round(monthTollFeeCashSum * 100.0) / 100.0;
            monthTollFeeEtcSum = Math.round(monthTollFeeEtcSum * 100.0) / 100.0;
            monthForfeitSum = Math.round(monthForfeitSum * 100.0) / 100.0;
            monthInformationFeeSum = Math.round(monthInformationFeeSum * 100.0) / 100.0;
            monthRepairTruckFeeSum = Math.round(monthRepairTruckFeeSum * 100.0) / 100.0;
            monthServiceStationRepairSum = Math.round(monthServiceStationRepairSum * 100.0) / 100.0;
            monthInsurancePremiumSum = Math.round(monthInsurancePremiumSum * 100.0) / 100.0;
            monthTireFeeSum = Math.round(monthTireFeeSum * 100.0) / 100.0;
            monthOtherFeeSum = Math.round(monthOtherFeeSum * 100.0) / 100.0;
            monthExpenseTotalSum = Math.round(monthExpenseTotalSum * 100.0) / 100.0;
            monthProfitSum = Math.round(monthProfitSum * 100.0) / 100.0;

            /**
             * 月利润单，数据准备
             */
            tProfitBillInfo.setProfitBillNumber(monthProfitBillNumber);
            tProfitBillInfo.setProfitBillDate(Date.valueOf(lastMonthDate));
            tProfitBillInfo.setStatisticFieldName(freightSettlementCustomer);
            tProfitBillInfo.setFreightWeightSum(monthFreightWeightSum);
            tProfitBillInfo.setFreightAmountSum(monthFreightAmountSum);
            tProfitBillInfo.setSalaryAmountSum(monthSalaryAmountSum);
            tProfitBillInfo.setCashSupplementGasSum(monthCashSupplementGasSum);
            tProfitBillInfo.setAddGasFeeSum(monthAddGasFeeSum);
            tProfitBillInfo.setTollFeeCashSum(monthTollFeeCashSum);
            tProfitBillInfo.setTollFeeEtcSum(monthTollFeeEtcSum);
            tProfitBillInfo.setForfeitSum(monthForfeitSum);
            tProfitBillInfo.setInformationFeeSum(monthInformationFeeSum);
            tProfitBillInfo.setRepairTruckFeeSum(monthRepairTruckFeeSum);
            tProfitBillInfo.setServiceStationRepairSum(monthServiceStationRepairSum);
            tProfitBillInfo.setInsurancePremiumSum(monthInsurancePremiumSum);
            tProfitBillInfo.setTireFeeSum(monthTireFeeSum);
            tProfitBillInfo.setOtherFeeSum(monthOtherFeeSum);
            tProfitBillInfo.setExpenseTotalSum(monthExpenseTotalSum);
            tProfitBillInfo.setProfitSum(monthProfitSum);

            tProfitBillInfo.setStatisticField("结费客户");
            tProfitBillInfo.setStatisticGranularity("月账单");
            tProfitBillInfo.setCreateBy("system");
            tProfitBillInfo.setCreateTime(DateUtils.getNowDate());

            /**
             * 月利润单，插入数据
             */
            tProfitBillInfoService.insertTProfitBillInfo(tProfitBillInfo);
            sleep(300);

            /**
             * 获取插入记录，ID
             */
            monthProfitBillId = tProfitBillInfoService.selectProfitBillIdByProfitBillNumber(monthProfitBillNumber);
//            logger.debug("OOO " + "salaryBillId = " + salaryBillId);

            /**
             * monthFreightAmountSum清零
             */
            monthFreightWeightSum = 0.0;
            monthFreightAmountSum = 0.0;
            monthSalaryAmountSum = 0.0;
            monthCashSupplementGasSum = 0.0;
            monthAddGasFeeSum = 0.0;
            monthTollFeeCashSum = 0.0;
            monthTollFeeEtcSum = 0.0;
            monthForfeitSum = 0.0;
            monthInformationFeeSum = 0.0;
            monthRepairTruckFeeSum = 0.0;
            monthServiceStationRepairSum = 0.0;
            monthInsurancePremiumSum = 0.0;
            monthTireFeeSum = 0.0;
            monthOtherFeeSum = 0.0;
            monthExpenseTotalSum = 0.0;
            monthProfitSum = 0.0;


            /**
             * 利润运单关联表，数据插入
             */
//            logger.debug("OOO " + "wayBillIds = " + wayBillIds);
            tProfitWayBillRelService.insertTProfitWayBillRelExt(monthProfitBillId, wayBillIds);
            wayBillIds.clear();
        }

        logger.debug("统计，月利润单，月账单，按结算客户，结束 ...");
    }

}
